<?php
namespace Admin\Controller;
use Think\Controller;
class CateController extends Controller {
	public $catedata=[];
	public $goodsdata=[];

	public function msg($msg){
		echo '插入数据'.$msg;
	}

	// 添加cateadd方法
    public function cateadd(){
    	// if(!IS_POST){
	    // 	$this->display();
    	// }else{
	    	$cate = D('Cate');
	    	// $GoodsModel = D('goods');
	    	$this->catedata['cat_name'] = I('cat_name');
	    	$this->catedata['parent_id'] = I('parent_id');
	    	// $this->goodsdata['goods_number'] = I('measure_unit');
	    	var_dump($this->catedata);
	    	$cate->add($this->catedata);
	    // }
	    $this->display();
    }

    // 添加catelist方法
    public function catelist(){
    	$cate = D('Cate');//查cate表
        $cates = $cate->getTree();
        // var_dump($cates);
        $this->assign('cats',$cates);
        $this->display();
    }
    //修改栏目方法
    public function catedit(){
        $cate = D('Cate');

        $cats = $cate->getTree();
    	$this->assign('cats',$cats);

    	$row = $cate->find(I('get.cat_id'));//点击时获取cat_id,方便显示在修改框内

    	if(empty($row)){//如输入的cat_id不存在(非法),则跳转到catlist
    		$this->redirect('Admin/Cate/catelist');
    	}
    	if(!IS_POST){
    		$this->assign('row',$row);
    		$this->display();
		}else{
			$cate->cat_name=I('post.cat_name');
			$cate->intro=I('post.intro');
			// echo $cate->save()?'成功':'失败';
			$cate->save()?$this->msg('成功'):$this->msg('失败');
		}
		// $this->add($cate);   
    }

    public function catedel(){
        $cate = D('Cate');
        $cate->delete(I('get.cat_id'));
        $this->redirect('catelist');
    }
}